package com.example.villagemarket;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import android.location.Criteria;
import android.location.LocationManager;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

 
public class LocationFragment extends Fragment implements OnMapClickListener,
OnMapLongClickListener, LocationListener, 
android.location.LocationListener {
	
	private GoogleMap googleMap;
	
	private static View view;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	//This if statement and try/catch prevent the map fragment from being
    	//inflated twice.
    	if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.activity_location_fragment, container, false);
        } catch (InflateException e) {
            /* map is already there, just return view as it is */
        }
        
        return view;
        

    }
    
    

    
    private void initilizeMap() {
		if (googleMap == null) {
			googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
					R.id.mapFrag)).getMap();

			googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
			
			googleMap.setOnInfoWindowClickListener(
					  new OnInfoWindowClickListener(){
					    public void onInfoWindowClick(Marker marker){
							Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/dir//806+S+Main+St,+Fond+du+Lac,+WI+54935/@43.756631,-88.449402,12z/data=!4m8!4m7!1m0!1m5!1m1!1s0x8804750fdb0443c7:0x2b95535f0a22622!2m2!1d-88.4493393!2d43.7536801"));
							startActivity(browserIntent);
					    }
					  }
					);

			LatLng villageMarketLocation = new LatLng(43.756631, -88.449402);

			CameraPosition cameraPosition = new CameraPosition.Builder()
					.target(villageMarketLocation).zoom(16).build();

			// adds marker for current location
			googleMap.addMarker(new MarkerOptions().position(
					villageMarketLocation).title("Directions")).showInfoWindow();

			
			googleMap.animateCamera(CameraUpdateFactory
					.newCameraPosition(cameraPosition));
			// check if map is created successfully or not
			if (googleMap == null) {
				//Toast.makeText(getApplicationContext(),
						//"Sorry! unable to create maps", Toast.LENGTH_SHORT);

			}

		}
	}

	@Override
	public void onResume() {
		super.onResume();
		initilizeMap();
	}



	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLocationChanged(android.location.Location location) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMapLongClick(LatLng point) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMapClick(LatLng point) {
		// TODO Auto-generated method stub

	}

 
}